package com.elvin.platform.service;

import com.github.qcloudsms.SmsSingleSender;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class TencentSmsService {

  @Value("${tencent.sms.appId}")
  private int appId;

  @Value("${tencent.sms.appKey}")
  private String appKey;

  @Value("${tencent.sms.templateId}")
  private int templateId;

  @Value("${tencent.sms.smsSign}")
  private String smsSign;

  /**
   * 发送短信验证码
   * @param phone 手机号码
   * @param code 验证码
   * @return
   */
  public void sendSmsCode(String phone, String code) {
    try {
      // 创建短信发送对象
      SmsSingleSender sender = new SmsSingleSender(appId, appKey);
      // 创建短信发送参数
      ArrayList<String> params = new ArrayList<String>();
      params.add(code);
      params.add("5");
      // 发送短信
      System.out.println(smsSign);
      sender.sendWithParam("86", phone, templateId, params, smsSign, "", "");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
